package com.zwh.application;
//接收前端问题的


import com.zwh.chatgpt.test.domain.ai.IOpenAI;
import com.zwh.chatgpt.test.domain.zsxq.IZsxqAi;
import com.zwh.entity.Question;
import com.zwh.service.UserService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

//问答任务
@EnableScheduling
@Configuration
@RestController
@RequestMapping()
public class AiController {
    private Logger logger = LoggerFactory.getLogger(AiController.class);//添加日记

    @Value("${chatgpt-ai.groupId}")//我真的服了，记得这里加的是大括号
    private  String groupId;

    @Value("${chatgpt-ai.cookie}")
    private  String cookie;

    @Resource
    private IZsxqAi iZsxqAi;

    @Resource
    private IOpenAI openAI;

    public static class UserInputDto {

        private String input;

        // getters and setters
        public String getInput() {
            return input;
        }
        public void setInput(String input) {
            this.input = input;
        }

    }

    @Autowired
    private UserService userService;

    //拿到用户输入的问题,并将数据存储到数据库
// 在类级别定义一个静态队列
    private static Queue<String> questionQueue = new ConcurrentLinkedQueue<>();

    // 修改 questionInput 方法
    @RequestMapping("/input")
    public String questionInput(@RequestBody AiController.UserInputDto userInput) {
        Question question = new Question();
        question.setQuestion_content(userInput.getInput());
        userService.questionkeep(question);
        // 同时将问题添加到队列
        questionQueue.add(userInput.getInput());

        String answer = null;
        try {
            // 从队列中获取问题
            String question1 = questionQueue.poll();
            if (question1 != null) {
                answer = openAI.doDouBao(question1);
                logger.info("豆包返回的内容：{}", answer);

            }
        } catch (Exception e) {
            logger.error("自动回答问题异常", e);
        }
        return answer;
    }


}
